package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.SourceType;

import java.io.IOException;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 */
public class ProductDetailPage extends Utils {
    //获取”全部分类”页面的url
    public static String url = "http://49.235.61.184/?s=category/index.html";

    public ProductDetailPage() {
        super(url);
    }


    public void checkProductDetailPage() throws IOException, InterruptedException {

        //获取原来句柄
        String curWindow = driver.getWindowHandle();
        //点击时尚服饰，分类跳转时尚服饰
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-top > div > div > div > div > ul > li:nth-child(2) > div")).click();
        //点击女装
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-left > div > div > ul > li:nth-child(2)")).click();
        //点击连衣裙
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-title.am-scrollable-horizontal > div > a.am-radius.active")).click();
        Thread.sleep(2000);
        //点击图片,跳转到商品的详情页面
        driver.findElement((By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-item > div > ul > li:nth-child(1) > div > a > img"))).click();
        Thread.sleep(3000);
        //获取所有页面句柄
        Set<String> allWindows = driver.getWindowHandles();
        //切换到商品详情页面
        for (String window:allWindows) {
            if (!window.equals(curWindow)){
                driver.switchTo().window(window);
            }
        }

        /*检查页面元素*/
        //检查图片
        driver.findElement(By.cssSelector("#clearcontent > div.box.am-radius > div.tb-booth.tb-pic.am-radius > img"));
        //检查原价
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > div > div > div.tb-detail-price-content > div.items.goods-price.goods-original-price"));
        //检查售价
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > div > div > div.tb-detail-price-content > div.items.goods-price.goods-sale-price"));
        //检查输入框
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > dl > dd > div.theme-popover > div.theme-popbod.dform.am-background-white > form > div.theme-signin-left > div.theme-options.am-flex.am-flex-row.am-flex-items-center > dd > div > input"));
        //检查手机购买二维码
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > div > div > div.goods-qrcode > div > a > i"));
        //检查”立即购买“按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.buy-nav > div.buy-submit-container.buy-submit-container-number-2 > button.am-radius.am-btn.am-btn-primary.buy-submit.buy-event.login-event"));
        //检查”加入购物车“按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.buy-nav > div.buy-submit-container.buy-submit-container-number-2 > button.am-radius.am-btn.am-btn-secondary.cart-submit.buy-event.login-event"));
        //检查”收藏“按钮
        driver.findElement(By.cssSelector("#clearcontent > div.photo-bottom.am-cf > span > em"));
        //检查”详情“
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.introduce.am-container.am-radius.am-margin-top-main > div.introduce-main.no-left-goods > div > div.am-sticky-placeholder > ul > li.goods-middle-nav-detail.am-active > a"));
        //检查评价
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.introduce.am-container.am-radius.am-margin-top-main > div.introduce-main.no-left-goods > div > div.am-sticky-placeholder > ul > li.goods-middle-nav-comments > a"));
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());


        /*检查页面功能*/
        //立即购买
        //选择尺码-M码
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > dl > dd > div.theme-popover > div.theme-popbod.dform.am-background-white > form > div.theme-signin-left > div.sku-container > div > ul > li:nth-child(1) > span")).click();
        //点击“+”按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > dl > dd > div.theme-popover > div.theme-popbod.dform.am-background-white > form > div.theme-signin-left > div.theme-options.am-flex.am-flex-row.am-flex-items-center > dd > div > button.am-input-group-label.am-radius.operate-number-inc")).click();
        //点击“立即购买”按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.buy-nav > div.buy-submit-container.buy-submit-container-number-2 > button.am-radius.am-btn.am-btn-primary.buy-submit.buy-event.login-event")).click();
        Thread.sleep(1000);
        //获取页面标题
        String title = driver.getTitle();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        assert title.equals("订单确认 - 网站名称 - SEO标题");
        //后退
        driver.navigate().back();

        //加入购物车
        //获取加入购物车数量
        String num = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div > a > strong")).getText();
        //选择尺码-L码
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > dl > dd > div.theme-popover > div.theme-popbod.dform.am-background-white > form > div.theme-signin-left > div.sku-container > div > ul > li:nth-child(2)")).click();
        //清空输入框
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > dl > dd > div.theme-popover > div.theme-popbod.dform.am-background-white > form > div.theme-signin-left > div.theme-options.am-flex.am-flex-row.am-flex-items-center > dd > div > input")).clear();
        //输入要购买的个数
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > dl > dd > div.theme-popover > div.theme-popbod.dform.am-background-white > form > div.theme-signin-left > div.theme-options.am-flex.am-flex-row.am-flex-items-center > dd > div > button.am-input-group-label.am-radius.operate-number-inc")).click();
        //点击“加入购物车”按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.buy-nav > div.buy-submit-container.buy-submit-container-number-2 > button.am-radius.am-btn.am-btn-secondary.cart-submit.buy-event.login-event")).click();
        Thread.sleep(5000);
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //获取加入购物车数量*/
        String num1 = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div > a > strong")).getText();
        assert !num1.equals(num);

        //点击收藏
        driver.findElement(By.cssSelector("#clearcontent > div.photo-bottom.am-cf > span")).click();
        //屏幕截图
        Thread.sleep(1000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        String str1 = driver.findElement(By.cssSelector("#clearcontent > div.photo-bottom.am-cf > span > em")).getText();
        assert str1.equals("已收藏");


        Thread.sleep(3000);
        //取消收藏
        driver.findElement(By.cssSelector("#clearcontent > div.photo-bottom.am-cf > span")).click();
        //屏幕截图
        Thread.sleep(1000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        Thread.sleep(2000);
        String str2 = driver.findElement(By.cssSelector("#clearcontent > div.photo-bottom.am-cf > span > em")).getText();
        assert str2.equals("收藏");

        //关闭当前页面
        driver.close();
        //切换到原来页面
        driver.switchTo().window(curWindow);

    }

}
